package com.example.ccj.u;

import android.os.Vibrator;

import androidx.annotation.RequiresPermission;

public final class VibrateUtils {
    private static Vibrator vibrator;

    private VibrateUtils() {
        throw new UnsupportedOperationException("u can't instantiate me...");
    }

    @RequiresPermission("android.permission.VIBRATE")
    public static void vibrate(long milliseconds) {
        Vibrator vibrator = getVibrator();
        if (vibrator != null) {
            vibrator.vibrate(milliseconds);
        }
    }

    @RequiresPermission("android.permission.VIBRATE")
    public static void vibrate(long[] pattern, int repeat) {
        Vibrator vibrator = getVibrator();
        if (vibrator != null) {
            vibrator.vibrate(pattern, repeat);
        }
    }

    @RequiresPermission("android.permission.VIBRATE")
    public static void cancel() {
        Vibrator vibrator = getVibrator();
        if (vibrator != null) {
            vibrator.cancel();
        }
    }

    private static Vibrator getVibrator() {
        if (vibrator == null) {
            vibrator = (Vibrator)Utils.getApp().getSystemService("vibrator");
        }

        return vibrator;
    }
}
